package io.kimmking.rpcfx.client;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponse;


public class HttpClientInboundHandler extends ChannelInboundHandlerAdapter {

    public static ThreadLocal<String> result = new ThreadLocal<>();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("--------------分割线--------------");
        if (msg instanceof HttpResponse)
        {

            HttpResponse response = (HttpResponse) msg;
            System.out.println("CONTENT_TYPE:" + response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
        }
        if(msg instanceof HttpContent)
        {
            System.out.println("--");
            System.out.println(Thread.currentThread().getName());
            HttpContent content = (HttpContent)msg;
            ByteBuf buf = content.content();
            HttpClientInboundHandler.result.set(buf.toString(io.netty.util.CharsetUtil.UTF_8));
            System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
            buf.release();
        }
        if (msg instanceof FullHttpRequest) {
            System.out.println("full httpRequest");
            System.out.println(msg);
        }
    }
}
